package edu.hcnu.controller;

import edu.hcnu.common.ServerResponse;
import edu.hcnu.domain.entity.YiQing;
import edu.hcnu.service.IYiQingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: Administrator
 * @time: 2023/2/9 23:27
 */
@RestController
@CrossOrigin
@RequestMapping("/yiqing")
public class YiQingController {

    @Autowired
    private IYiQingService service;

    @GetMapping("test")
    public ServerResponse test() {
        return ServerResponse.success(200, "测试成功");
    }

    @GetMapping("list")
    public ServerResponse list(@RequestParam("current") int current, @RequestParam("size") int size, @RequestParam("key") String key) {
        List<YiQing> res = service.queryPage(current, size, key);

        if (res != null) {
            return ServerResponse.success(200, "查询成功", res);
        }
        return ServerResponse.fail(400, "查询失败");
    }

    @PostMapping("add")
    public ServerResponse add(@RequestBody YiQing yiQing) {
        Boolean res = service.add(yiQing);
        System.out.println("res = " + res);
        if (res!=null && !res) {
            return ServerResponse.success(200, "添加成功", true);
        }
        return ServerResponse.fail(400, "添加失败");
    }

    @DeleteMapping("delete/{id}")
    public ServerResponse delete(@PathVariable("id") int id) {
        Boolean res = service.deleteById(id);
        if (res != null && !res) {
            return ServerResponse.success(200, "删除成功", true);
        }
        return ServerResponse.fail(400, "删除失败");
    }

    @GetMapping("count")
    public ServerResponse count(@RequestParam("key") String key) {
        Integer res = service.count(key);
        if (res != null) {
            return ServerResponse.success(200, "查询数量成功", res);
        }
        return ServerResponse.fail(400, "查询数量失败");
    }

}
